package com.zzb.servlet.navigation;


import com.alibaba.fastjson.JSONObject;
import com.zzb.DTO.ResultDTO;
import com.zzb.VO.NavigationVO;
import com.zzb.service.NavigationService;
import com.zzb.service.impl.NavigationServiceImpl;
import com.zzb.util.Tool;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet("/back/navigation/update")
public class NavigationUpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //乱码处理
        req.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        resp.setCharacterEncoding(StandardCharsets.UTF_8.toString());

        //得到前端数据
        String name = req.getParameter("name");
        String id = req.getParameter("id");
        String enable = req.getParameter("enable");
        String isShow = req.getParameter("isShow");
        String remark = req.getParameter("remark");
        String href = req.getParameter("href");


        //对前端数据进行封装
        NavigationVO navigationVO = new NavigationVO();
        navigationVO.setId(Integer.valueOf(id));
        navigationVO.setName(Tool.nullToStr(name));
        navigationVO.setRemark(Tool.nullToStr(remark));
        navigationVO.setHref(Tool.nullToStr(href));
        navigationVO.setEnable(Integer.valueOf(Tool.nullToNum(enable)));
        navigationVO.setIsShow(Integer.valueOf(Tool.nullToNum(isShow)));
        //调用服务逻辑
        NavigationService navigationService = new NavigationServiceImpl();

        //得到返回值
        ResultDTO resultDTO = navigationService.update(navigationVO);
        //将数据响应给前端
        resp.getWriter().println(JSONObject.toJSONString(resultDTO));
    }
}
